import React from 'react';
import PropTypes from 'prop-types';
import ListEmpty from 'atComponents/ListEmpty';
import track from 'utils/track';
import { Button } from '@teamix/ui';
import intl from '../../../../../../../../../locale';

export default class WarningPage extends React.Component {
  static propTypes = {
    buildResult: PropTypes.object,
  };

  static defaultProps = {
    buildResult: null,
  };

  handleManual = () => {
    this.props.startScan();
    track({
      action: 'manual_trigger_scan',
      page: 'Security-Sensitive-Info-Detection',
      type: '',
      control: '',
    });
  };

  render() {
    const { buildResult, incompleteData, issueCount, pipelineId, initial } = this.props;
    if (!buildResult && !incompleteData) {
      return (
        <ListEmpty
          type="project"
          className="no-content"
          content=""
          description={
            initial
              ? intl.get({
                  id: 'code-assets.pinpoint.containers.warningPage.TheCurrentBranchIsPerforming',
                  defaultMessage: '当前分支正在执行源码漏洞检测',
                })
              : intl.get({
                  id: 'code-assets.pinpoint.containers.warningPage.TheCurrentBranchHasNot',
                  defaultMessage: '当前分支暂未执行源码漏洞检测',
                })
          }
          imgType="search"
          btns={[
            <Button
              type="primary"
              component="a"
              target="_blank"
              href={`https://flow.aliyun.com/pipelines/${pipelineId}/history`}
            >
              {initial
                ? intl.get({
                    id: 'code-assets.pinpoint.containers.warningPage.ViewTheFlow',
                    defaultMessage: '前往 Flow 查看',
                  })
                : intl.get({
                    id: 'code-assets.pinpoint.containers.warningPage.GoToTheFlow',
                    defaultMessage: '前往 Flow 执行',
                  })}
            </Button>,
          ]}
        />
      );
    }

    // count为0， 并且buidlResult.id < incompleteData.id incompleteData结果为准 ,否则buildResult优先
    const isInCompleteDataFirst =
      buildResult &&
      incompleteData &&
      buildResult.id &&
      incompleteData.id &&
      buildResult.id < incompleteData.id &&
      (issueCount === 0 || incompleteData.state.toLowerCase() === 'running');
    // 都有数据显示buildResult优先
    if (!isInCompleteDataFirst && buildResult && buildResult.state.toLowerCase() === 'pass') {
      return (
        <ListEmpty
          type="project"
          content={intl.get({
            id: 'code-assets.pinpoint.containers.warningPage.NoSourceCodeVulnerabilityDetection',
            defaultMessage: '暂无源码漏洞检测问题',
          })}
          description={intl.get({
            id: 'code-assets.pinpoint.containers.warningPage.GreatKeepTryingToKeep',
            defaultMessage: '太棒了，继续努力保持哦',
          })}
          imgType="500"
        />
      );
    }
    // fail && warning  && count为0,表示有旧数据
    if (
      !isInCompleteDataFirst &&
      buildResult &&
      (buildResult.state.toLowerCase() === 'fail' || buildResult.state.toLowerCase() === 'warning')
    ) {
      return (
        <ListEmpty
          type="project"
          className="no-content"
          content=""
          description={intl.get({
            id: 'code-assets.pinpoint.containers.warningPage.PerformAScanToObtain',
            defaultMessage: '执行扫描以获取当前分支的最新检测结果',
          })}
          image="https://img.alicdn.com/tfs/TB1jAyCzAT2gK0jSZFkXXcIQFXa-600-600.png"
        />
      );
    }

    if (incompleteData && incompleteData.state.toLowerCase() === 'error') {
      return (
        <ListEmpty
          type="project"
          className="no-content"
          content=""
          description={intl.get({
            id: 'code-assets.pinpoint.containers.warningPage.AnErrorOccurredWhileDetecting',
            defaultMessage: '源码漏洞检测执行异常',
          })}
          imgType="search"
        />
      );
    }
    if (
      incompleteData &&
      (incompleteData.state.toLowerCase() === 'running' ||
        incompleteData.state.toLowerCase() === 'waiting')
    ) {
      return (
        <ListEmpty
          type="project"
          className="no-content no-btns "
          content=""
          description={intl.get({
            id: 'code-assets.pinpoint.containers.warningPage.SourceCodeVulnerabilityDetectionAnd',
            defaultMessage: '源码漏洞检测扫描中…',
          })}
          imgType="search"
        />
      );
    }
    return null;
  }
}
